<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Example
 *
 * This is an example of a few basic user interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		Phil Sturgeon
 * @link		http://philsturgeon.co.uk/code/
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class BaseService extends REST_Controller
{
	function __construct(){
		parent::__construct();
		$this->load->model('basemodel','',TRUE);
		$this->load->helper('date');
	}
	
	function insert_put()
	{
		$modelName = $this->put('name');
		$dataArray = $this->put('data');
		$dataArray['createdDate'] = date('Y-m-d H:i:s');
		$dataArray['updatedDate'] = date('Y-m-d H:i:s');
		$result = $this-> basemodel-> insert($modelName, $dataArray);
		if($result)
		{
			$this->response($result, 200);
		}
		else
		{
			$this->response(array('error' => 'Couldn\'t insert user.'), 404);
		}
	}
	
	function update_post()
	{
		$modelName = $this->post('name');
		$id = $this->post('id');
		$dataArray = $this->post('data');
		$dataArray['updatedDate'] = date('Y-m-d H:i:s');
		$result = $this->basemodel->update($modelName, $id, $dataArray);
		
		if($result)
		{
			$this->response($result, 200);
		}
		else 
		{
			$this->response(array('error' => 'Couldn\'t insert user.'), 404);
		}
	}
	
	function gets_get()
	{
		$this->basemodel->select = $this->get('select');
		$this->basemodel->from = $this->get('from');
		$this->basemodel->join = $this->get('join');
		$this->basemodel->joinon = $this->get('joinon');
		$this->basemodel->joinoption = $this->get('joinoption');
		$this->basemodel->where = $this->get('where');
		$this->basemodel->groupby = $this->get('groupby');
		$this->basemodel->having = $this->get('having');
		$this->basemodel->orderby = $this->get('orderby');
		$this->basemodel->limit = $this->get('limit');
		
		$model = $this->basemodel->gets();

		if($model)
		{
			$this->response($model, 200); // 200 being the HTTP response code
		}
	
		else
		{
			$this->response(array('error' => 'Couldn\'t find any users!'), 404);
		}
	}
	
	function get_get()
	{
		$model = $this->basemodel->getById($this->get('name'), $this->get('id'));

		if($model)
		{
			$this->response($model, 200); // 200 being the HTTP response code
		}
	
		else
		{
			$this->response(array('error' => 'Couldn\'t find any users!'), 404);
		}
	}
}